CmdUtils.CreateCommand({
  names: ["twittercounter"],
  icon: "http://twittercounter.com/images/favicon.png",
  description: "Twittercounter is a free web-service that provides FREE statistics on a Twitter user",  
  help: "Usage is simply: twittercounter <twitter username> <br/><br/>" + "Eg. twittercounter mskadu",
  author: {name: "MK", email: "mskadu+ubiquity@gmail.com"},
  license: "GPL",
  homepage: "http://twittercounter.com/",
  arguments: [{role: 'object', nountype: noun_arb_text}],
  preview: function preview(pblock, args) {
    pblock.innerHTML = "Display Twitter Follower Stats for <b>" + args.object.html + "</b>.";

    if( args.object.text)
    {
      var escapedText = escape( args.object.text ); //sanitize input text
      var params = { username: escapedText, 
                     output: 'json' }; //for the moment, use the default duration of 14 days
      var api_end_point = "http://twittercounter.com/api/";
      CmdUtils.previewGet( pblock, 
                           api_end_point, 
                           params,
                           function( twitterOutput ) 
                           {
                              CmdUtils.log( twitterOutput );

                              /*
                              for( var key in twitterOutput )
                              {
                                CmdUtils.log( key, twitterOutput[ key ] );
                              }*/

                              if( twitterOutput['error']==null )
                              {
                                //call was ok 
                                pblock.innerHTML += "<p>" +
                                                    "<table border=1 width='50%'><tr><td colspan='2' align=center>TwitterCounter Stats Details</td></tr>" +
                                                    "<tr><td>Current Followers</td><td>" + twitterOutput['followers_current'] + "</td></tr>" +
                                                    "</table></p>";
                              }
                              else 
                              {
                                //display error message
                                var errMsg = twitterOutput['error'];
                                pblock.innerHTML += "<p>Error: " + errMsg + "</p>" ;
                              }
                          }
                         ,
                         "json"
      );
    }                   
  },
  execute: function execute(args) {
             var url = "http://twittercounter.com/";
             if( args.object.text )
             {
               url+= escape( args.object.text );
             }

             Utils.openUrlInBrowser( url );
           }
});

